<template>
  <div class="AddOrUpdate_main add_container tenantMenu_main main_container user_container">
    <div style="margin-right:0;padding-top:0" class="main_main">
      <div class="main_main_container">
        <div class="main_main_left">
          <div class="title" />
          <div class="main_main_search_input">
            <el-input
              v-model="filterText"
              style="width:195px;heigth:30px"
              clearable
              placeholder="请输入保养模版"
              @keyup.enter.native="filterNode"
              @clear="getList"
            />
            <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="filterNode" />
          </div>
          <div class="titleText">保养模版</div>
          <div class="main_main_left_top">
            <div class="leftClass">
              <div v-for="item in searchData" :key="item.id" :class="[item.id==rowId?'activeClass':'']" class="rowClass" @click.stop="getTemplateInfo(item)">
                <div v-if="item.rowClassTwo == false">{{ item.name }}</div>
                <div v-else>
                  <el-tooltip :content="item.name" placement="top">
                    <div>{{ item.name.substring(0,10)+'...' }}</div>
                  </el-tooltip>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div class="main_main_right">
          <div style="padding-right:20px" class="SearchBar_main main_main_search">
            <div style="padding-left:0" class="main_main_search_item">
              <div class="formItem_header">
                <div class="formItem_header_operate">
                  <a class="formItem_header_operate_items" @click="add">
                    <svg-icon icon-class="新增" />
                    <div class="item_name">新增</div>
                  </a>
                </div>
              </div>
            </div>
            <div class="main_main_search_item">
              <el-input v-model="pageData.searchValue" style="width:200px;margin-right:10px" placeholder="请输入保养项目或内容" clearable @keyup.enter.native="getLists" @clear="getTemplateInfo(rowData)" />
              <el-button style="background:#387bfc;width:30px;" icon="el-icon-search" @click.stop="getLists" />
            </div>
          </div>
          <div ref="table_container" style="margin-top:20px" class="table_container">
            <el-table
              ref="table"
              :data="tableData"
              stripe
              border
              :height="tableHeight"
              style="width: 99.9%"
              row-key="menuId"
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
              @sort-change="
                (column) => {
                  sortChange(column, this);
                }
              "
              @selection-change="
                (val) => {
                  selectionChange(val, this);
                }
              "
            >
              <el-table-column prop="sort" label="序号" width="70">
                <template slot-scope="scope">
                  <div v-if="!scope.row.isAdd">{{ scope.row.sort }}</div>
                  <div v-else />
                </template>
              </el-table-column>
              <el-table-column prop="name" label="保养项目">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.name" />
                  <!-- <el-input v-model="scope.row.name" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column prop="content" label="保养内容">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.content" />
                  <!-- <el-input v-model="scope.row.content" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column prop="way" label="保养方式">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.way" />
                  <!-- <el-input v-model="scope.row.way" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column prop="time" label="时间 ( S )" width="80">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.time" />
                  <!-- <el-input v-model="scope.row.time" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column prop="criterion" label="判断标准">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.criterion" />
                  <!-- <el-input v-model="scope.row.criterion" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column prop="time" label="至少上传张数" width="120">
                <template slot-scope="scope">
                  <DivTextarea :row-value="scope.row.minPage?scope.row.minPage+'':''" />
                  <!-- <el-input v-model="scope.row.time" style="margin:10px 0;" type="textarea" autosize readonly /> -->
                </template>
              </el-table-column>
              <el-table-column label="附件" width="80">
                <template slot-scope="scope">
                  <FileManage
                    :file-names.sync="scope.row.fileName"
                    :file-real-names.sync="scope.row.fileRealName"
                    :file-urls.sync="scope.row.fileUrl"
                    :row-index="scope.$index"
                    :readonly="true"
                  />
                </template>
              </el-table-column>
            </el-table>
          </div>

          <!--          <div style="margin:0 .625rem .625rem 0">-->
          <!--            <pagination-->
          <!--              :total="total"-->
          <!--              :page.sync="pageData.pageNum"-->
          <!--              :limit.sync="pageData.pageSize"-->
          <!--              :selected-num="selectionsAll.length"-->
          <!--              @pagination="getList"-->
          <!--            />-->
          <!--          </div>-->
        </div>
      </div>
    </div>
    <div style="background:white;border-top:.0625rem solid #ECECEC;" class="main_bottom">
      <el-button type="cancel" @click.stop="cancel">取消</el-button>
      <el-button @click.stop="onSubmit">确定</el-button>
    </div>
  </div>
</template>

<script>
import DivTextarea from '@/components/JLink/common/DivTextarea.vue'
import {
  list,
  getInfo
} from '@/api/equipmentManage/equipmentMaintain/maintainTemplate'
import AddOrUpdate from './addOrUpdate' // 新增或编辑页面

export default {
  name: 'Index',
  components: {
    DivTextarea
  },
  props: {
    layerid: {},
    defaultValue: {},
    id: {},
    name: {}
  },
  data() {
    return {
      rowData: {},
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      loading: true,
      pageId: '保养模板', // 页面唯一标识
      pageData: {
        pageNum: 1,
        pageSize: 9999,
        orderByColumn: 'updateTime',
        isAsc: 'desc',
        searchValue: '',
        status: 1
      },
      rowId: '',
      total: 1, // 数据一共有多少条
      mainId: 'id', // 表格行ID
      tableHeight: '0', // 表格高度
      tableData: [], // 表格数据
      searchData: [],
      searchCloneData: [],
      filterText: '', // 树节点搜索字段
      tableColumns: [], // 表格实际用的字段
      searchColumns: [], // 搜索区实际用的字段
      selections: [], // 表格当前页所选中的行
      selectionsAll: [], // 表格所有页选中的行
      templateName: '',
      templateId: '',
      remark: '',
      allData: []
    }
  },
  mounted() {
    this.getList()
    this.$nextTick(() => {
      this.tableHeight = this.$refs.table_container.offsetHeight
    })
    // if (this.id) {
    //   this.rowId = this.id
    //   const value = { name: this.name, id: this.id }
    //   this.getTemplateInfo(value)
    // }
  },
  methods: {
    // 树节点搜索
    filterNode() {
      if (this.filterText && this.filterText !== '') {
        this.searchData = this.searchCloneData.filter((item) => item.name.indexOf(this.filterText) !== -1
        )
      } else {
        this.getList()
      }
    },
    getLists() {
      const arr = JSON.parse(JSON.stringify(this.selectionsAll))
      this.tableData = this.allData.filter(item => item.content.indexOf(this.pageData.searchValue) !== -1 || item.name.indexOf(this.pageData.searchValue) !== -1)
      this.selectionsAll = arr
      this.$nextTick(() => {
        this.tableData.forEach(ele => {
          this.$refs.table.toggleRowSelection(ele, true)
        })
      })
    },
    getList() {
      list(this.pageData).then((res) => {
        this.searchData = res.data
        this.searchData.forEach(item => {
          if (item.name.length > 10) {
            item.rowClassTwo = true
          } else {
            item.rowClassTwo = false
          }
        })
        this.getTemplateInfo(this.searchData[0])
        this.searchCloneData = JSON.parse(JSON.stringify(this.searchData))
        this.total = res.total
        this.loading = false
      })
    },
    // 查询详情
    getTemplateInfo(item) {
      console.log(item)
      this.rowData = item
      this.rowId = item.id
      this.templateName = item.name
      this.templateId = item.id
      this.selections = []
      this.selectionsAll = []
      this.tableData = []
      getInfo(item.id).then(res => {
        this.tableData = res.data.itemList
        this.allData = JSON.parse(JSON.stringify(res.data.itemList))
        this.remark = res.data.remark
        this.$nextTick(() => {
          this.tableData.forEach(ele => {
            this.$refs.table.toggleRowSelection(ele, true)
          })
        })
      })
    },
    add() {
      this.$layer.iframe({
        // 弹框
        content: {
          content: AddOrUpdate, // 传递的组件对象
          parent: this, // 当前的vue对象
          data: {} // props
        },
        area: ['1320px', '720px'],
        title: '新增保养模板', // 标题
        shadeClose: false // 点击遮罩是否关闭
      })
    },
    cancel() {
      this.$layer.close(this.layerid) // 关闭弹框
    },
    onSubmit() {
      if (JSON.stringify(this.rowData) == '{}') {
        this.$message.warning('请选择保养模版')
        return
      } else {
        this.$parent.showTemplate(this.rowData)
        this.$layer.close(this.layerid) // 关闭弹框
      }
    }
  }
}
</script>

<style lang="less" scoped>
/deep/ .accessory{
  color: #387bfc;
}
/deep/ .el-textarea__inner{
  border: none !important;
  background: none;
  overflow: hidden;
}
.titleText{
  height: 40px;
  line-height: 40px;
  font-size: 14px;
  color: #606266;
}
.main_main_search_input{
  margin-top: 20px;
  margin-bottom: 10px ;
 /deep/ .el-input__inner{
    height: 30px;
  }
}
.activeClass{
  background-color: rgba(56, 123, 252, 1);
  color: white;
}
  .leftClass {
      font-size: 14px;
  color: #606266;
    flex: 2;
    // margin-right: 45px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    // &:nth-child(even) {
    //   background: #148519;
    // }
    // &:nth-child(odd) {
    //   background: #eef0f4;
    // }
    .rowClass:hover{
      background-color: rgba(56, 123, 252, 0.5);
      cursor: pointer;
      color: white;
    }
    div {
      height: 40px;
      width: 100%;
      // text-align: center;
      padding-left:10px ;
      line-height: 40px;
    }
  }
.main_main {
  height: calc(100% - 50px);
}
.main_main_container {
  width: 100%;
  height: 100%;
  display: flex;

  .main_main_left {
    width: 250px;
    height: calc(100% - 20px);
    padding-right: 20px;
    .title {
      // padding: 10px 0;
      // border-bottom: 1px solid #387BFC;
      // margin-bottom: 10px;
            margin: 20px 0;

    }
    .main_main_left_top {
      width: 100%;
      height: calc(100% - 80px);
      overflow-y: auto;
      .el-tree {
        background-color: transparent;
      }
      /deep/ .el-tree-node__content:hover {
        background-color: rgba(56, 123, 252, 0.5) !important;
        color: white;
      }
    }
    .main_main_left_bottom {
      width: 100%;
      .main_main_left_bottom_item {
        background-color: rgba(74, 77, 82, 0.2);
        height: 40px;
        display: flex;
        justify-content: flex-start;
        align-items: center;
        padding-left: 24px;
      }
      .main_main_left_bottom_item:last-child {
        border-top: 1px solid white;
      }
      .main_main_left_bottom_item:hover {
        background-color: rgba(56, 123, 252, 0.5);
        color: white;
      }
      .active {
        .main_main_left_bottom_item {
          background-color: rgba(56, 123, 252, 1);
          color: white;
        }
      }
    }
  }
  .main_main_right {
    width: 75%;
    height: 100%;
    display: flex;
    flex-direction: column;
    border-left: 1px solid #ECECEC;

  }
}
.button{
    width: 100px;
    height: 30px;
    color: #fff;
    background-color: #387bfc;
    border-radius: 4px;
    text-align: center;
    line-height: 30px;
}
.formItem_header{
  justify-content: flex-start !important;
  align-items: flex-start !important;
  height: 40px !important;
}
.formItem_header_operate_items{
  height: 30px;
  display: flex;justify-content: center;align-items: center;
  padding: 0px 10px;font-size: 16px;border-radius: 4px;
  color: rgba(56, 123, 252, 1);background-color: rgba(235, 242, 255, 1);
  .item_name{
    font-size: 14px;padding-left: 10px;
  }
}
.formItem_header_operate_items:hover{
  background-color: rgba(235, 242, 255, 0.5);
}

</style>
